axios
  .get("/api/goods/detail", {
    params: {
      id: new URLSearchParams(document.location.search).get("goodsid"),
    },
    headers: {
      "X-Nideshop-Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjo2NSwiaWF0IjoxNjQxNDQxMjIwfQ.Z5VXSIIpsb-3AmuKb9wsa1fVtuxFQQSltzsJ8EeqkkA",
    },
  })
  .then((resp) => {
    // 轮播图
    const htmlGalleryStr = template("tpl-gallery", resp.data.data);
    // console.log(htmlGalleryStr);
    $(".swiper-wrapper").html(htmlGalleryStr);
    const mySwiper = new Swiper(".swiper", {
      autoplay: true,
      loop: true,
      pagination: {
        el: ".swiper-pagination",
      },
    });
    // 商品信息
    const htmlGoodsInfoStr = template("tpl-goodsinfo", resp.data.data);
    $(".goods-info .c").html(htmlGoodsInfoStr);

    // 商品详情
    $(".detail").html(resp.data.data.info.goods_desc);

    // 评论列表
    const htmlCommentStr = template("tpl-commentslist", resp.data.data.comment);
    $(".comments").html(htmlCommentStr);

    // 商品参数
    const htmlAttributeStr = template("tpl-attribute", resp.data.data);
    $(".goods-attr .l").html(htmlAttributeStr);

    // 问题列表
    const htmlIssueStr = template("tpl-issue", resp.data.data);
    $(".common-problem .b").html(htmlIssueStr);

    // 添加购物车弹窗
    const htmlCartStr = template("tpl-choose", resp.data.data);
    $(".img-info img").attr("src", resp.data.data.info.primary_pic_url);
    $(".spec-con").prepend(htmlCartStr);

    // 选择商品
    $(".spec-con").on("click", ".value", function () {
      $(this).addClass("selected").siblings().removeClass("selected");
      let spec_ids = [];
      $(".selected").each((index, ele) => {
        spec_ids.push($(ele).data("specid"));
      });
      let price = "";
      resp.data.data.productList.some((value) => {
        if (value.goods_specification_ids === spec_ids.join("_")) {
          price = value.retail_price;
          return true;
        }
      });
      console.log(price);
      $(".info .c .p").html(`价格：<em style="color:red;">￥${price}</em>`);
      $(".info .c .a").html(`已选择：${"128Gb"}`);
    });
    $(".selected").click();
    // 收藏状态
    let collectIconPath = "";
    if (resp.data.data.userHasCollect == 0) {
      collectIconPath = "./static/images/icon_collect.png";
    } else {
      collectIconPath = "./static/images/icon_collect_checked.png";
    }
    $(".bottom-btn .l-collect .icon").attr("src", collectIconPath);
  });

// “大家都在看”部分
axios
  .get("/api/goods/related", {
    params: {
      id: new URLSearchParams(document.location.search).get("goodsid"),
    },
  })
  .then((resp) => {
    const htmlRelatedStr = template("tpl-related", resp.data.data);
    $(".related-goods .b").html(htmlRelatedStr);
  });

// 收藏取消
$(".bottom-btn .l-collect").on("click", function () {
  axios({
    url: "/api/collect/addordelete",
    method: "POST",
    data: {
      typeId: 0,
      valueId: new URLSearchParams(document.location.search).get("goodsid"),
    },
    headers: {
      "X-Nideshop-Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjo2NSwiaWF0IjoxNjQxNDQxMjIwfQ.Z5VXSIIpsb-3AmuKb9wsa1fVtuxFQQSltzsJ8EeqkkA",
    },
  }).then((resp) => {
    console.log(resp.data.data.type);
    if (resp.data.data.type == "add") {
      $(".bottom-btn .l-collect .icon").attr(
        "src",
        "./static/images/icon_collect_checked.png"
      );
    } else if (resp.data.data.type == "delete") {
      $(".bottom-btn .l-collect .icon").attr(
        "src",
        "./static/images/icon_collect.png"
      );
    }
  });
});

// 购物车总数
axios
  .get("/api/cart/goodscount", {
    headers: {
      "X-Nideshop-Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjo2NSwiaWF0IjoxNjQxNDQxMjIwfQ.Z5VXSIIpsb-3AmuKb9wsa1fVtuxFQQSltzsJ8EeqkkA",
    },
  })
  .then((resp) => {
    console.log(resp.data.data.cartTotal.goodsCount);
    $(".cart-count").html(resp.data.data.cartTotal.goodsCount);
  });

$(".l-cart .box").on("click", () => {
  location.href = "./cart.html";
});
